<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class PZ_Controller extends Controller {
	protected $authfunctions = array();

	public function __construct() {
		parent::__construct();
	}

	public function beforeAction(){
		load_helper('global');
		$uapi = load_api('user');
		$api = load_api('setting');
		$ugroupapi = load_api('user/usergroup');
		$this->islogin = $uapi->isLogin();
		$this->isadmin = $uapi->isAdmin();
		$this->iseditor = $uapi->isEditor();
		$this->cuser = $uapi->getUserInfo();
		$this->mdl = str_replace('/', '', $this->module);
		$this->settings = $api->getGlobalSetting();
		$this->memperm = $ugroupapi->getGroupByid($this->cuser['member'],'member');
		$this->sysperm = $ugroupapi->getGroupByid($this->cuser['system'],'system');
		$this->speperm = $ugroupapi->getGroupByid($this->cuser['special'],'special');
		$this->checkStyle();
		if($this->settings['siteclosed']&&$this->sysperm['allowvisite']<200){
			$messages = $api->getMessageSetting();
			$heading = $messages['site_closed_heading']?$messages['site_closed_heading']:lang('site_closed_heading','tip');
			$message = $messages['site_closed_content']?$messages['site_closed_content']:lang('site_closed_content','tip');
			$this->message($heading,$message);
			return false;
		}
		if(in_array($this->method , $this->authfunctions)&&!$this->islogin){
			$this->jump(site_url('user/login'));
			return FALSE;
		}
		if($this->islogin){
			$this->shareSetting = $api->getSetting('share');
		}
		$this->ucopen = config_item('UC_OPEN','uc');
		$this->language = config_item('language');
		//$this->language = $this->settings['lang'];
		log_message('DEBUG','uc='.$this->ucopen.' language='.$this->language);
		$this->init();
		return true;
	}

	protected function init(){

	}


	private function checkStyle(){
		if($style = $this->input->cookie(config_item('cookie_prefix').'pz_style')){
			$existedstyles = $this->settings['existedstyles'];
			$us = explode(':', $style);
			if($existedstyles[$us[0]][$us[1]]){
				$this->theme = $us[0];
				$this->thcolor = $us[1];
			}else{
				$this->theme = $this->settings['style']?$this->settings['style']:'puzzing';
				$this->thcolor = $this->settings['color']?$this->settings['color']:'default';
			}
		}else{
			$this->theme = $this->settings['style']?$this->settings['style']:'puzzing';
			$this->thcolor = $this->settings['color']?$this->settings['color']:'default';
		}
		$this->stylecolors = $this->settings['colors']?$this->settings['colors']:false;
	}

	public function sessionput($key,$value){
		$this->session = &load_class('Session');
		$this->session->put($key,$value);
	}

	public function sessionget($key){
		$this->session = &load_class('Session');
		$this->session->get($key);
	}

	public function getcssvalue($key,$theme,$color){
		$api = load_api('setting');
		$styles = $api->getStyleSetting();
		return $styles[$theme][$color][$key];
	}

	public function param($key,$default=FALSE,$xss_clean = TRUE){
		$val = $this->input->get($key,$xss_clean);
		$val = (!$val&&!is_numeric($val))?$this->input->post($key,$xss_clean):$val;
		return ($val===false||$val==='')?$default:$val;
	}

	public function vcheck($key='login') {
		if($this->vcodesetting[$key]){
			$vcode = &load_class('Captcha');
			if($vcode->verify($this->param('verifycode'),false)) {
				return TRUE;
			}
			return FALSE;
		}
		return TRUE;
	}

	public function render($tpl,$vars=array()){
		return $this->template->display($tpl,$vars,$this->theme,$this->thcolor,true,0);
	}

	public function output($tpl,$vars=array(),$need_header_footer=true){
		$this->need_header_footer = $need_header_footer;
		if($need_header_footer){
			$this->rootCategories = (!$this->rootCategories)?load_api('category')->getRootCategories():$this->rootCategories;
			$this->storecategories = load_db('store/storecategory')->getAllCategories();
		}
		$this->template->display($tpl,$vars,$this->theme,$this->thcolor,false,0);
	}

	public function redirect($url,$message,$time=3){
		$this->template->display('common/redirect',array('url'=>$url,'message'=>$message,'time'=>$time),$this->theme,$this->thcolor,false,0);
	}

	public function jump($url){
		$this->template->content_display("<html><head><meta http-equiv='refresh' content='0;url={$url}'></head><body></body></html>");
	}

	public function message($heading,$message){
		$this->template->display('common/message',array('heading'=>$heading,'message'=>$message),$this->theme,$this->thcolor,false,0);
	}

	public function ajax_response($response){
		if($response['success']){
			$this->ajax_success_response($response['data'], $response['message']);
		}else{
			$this->ajax_failed_response($response['message']);
		}
	}

	public function ajax_success_response($data,$message=''){
		$response = array('success' => true, 'data' => $data, 'message'=>$message);
		$content = json_encode($response);
		$this->template->content_display($content);
	}

	public function ajax_failed_response($message='failed_msg',$data=null){
		$response = array('success' => false,'message'=>$message,'data'=>$data);
		$content = json_encode($response);
		$this->template->content_display($content);
	}

	public function ajax_echo($message){
		$this->template->content_display($message);
	}

	public function isChineseVersion(){
		return $this->language=='zh_cn';
	}

	public function relationView($user_id,$friend_id){
		$ptx_relationship = load_db('user/userrel');
		$status = $ptx_relationship->get_relation($user_id,$friend_id);
		$relation['user_id'] = $user_id;
		$relation['friend_id'] = $friend_id;
		$relation['status'] = $status;
		$data['relation'] = $relation;
		return $this->render('common/relation',$data);
	}

	public function seo_title($title,$needspace=true){
		$space = $needspace?' ':'';
		$this->page_title = sysSubStr($title,100,false).$space.$this->page_title;
	}

	public function seo_description($description){
		$this->page_description = sysSubStr($description,200,false).' '.$this->page_description;
	}

	public function seo_keyword($keyword){
		$this->page_keyword = sysSubStr($keyword,200,false).' '.$this->page_keyword;
	}

	public function load_seo($key){
		$settingapi = load_api('setting');
		$seosetting = $settingapi->getSetting('seo-'.$key);
		$this->seo_title($seosetting['seo_title']);
		$this->seo_keyword($seosetting['seo_keyword']);
		$this->seo_description($seosetting['seo_description']);
	}

}

?>